import { getConfigs, putConfig } from '../../../services/api';

export default {
  namespace: 'facilitatorDivided',

  state: {
    list: [],
  },

  effects: {
    *fetch({ payload: currentPage }, { call, put }) {
      const response = yield call(getConfigs, currentPage);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *update({ payload: values, callback }, { call }) {
      const { facilitatorCommissionRatio } = values;
      yield call(putConfig, 'facilitatorCommissionRatio', facilitatorCommissionRatio);
      if (callback) callback();
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        list: action.payload[0],
        about: action.payload[0],
        customerServicePhone: action.payload[1],
        facilitatorCommissionRatio: action.payload[2],
        helpAndFeedback: action.payload[3],
        memberCommissionRatio: action.payload[4],
        platformCommissionRatio: action.payload[5],
        user: action.payload[6],
      };
    },
    replace(state, action) {
      const { list } = state.data;
      list.splice(action.payload.editableIndex, 1, action.payload.response);
      return {
        ...state,
        data: {
          list,
          pagination: {
            ...state.data.pagination,
          },
        },
      };
    },
  },
};
